var util = require('../utils/util.js')

function getNewPage() {
  const oldPage = Page;

  return (args) => {
    const {
      onLoad,
      onReady,
      onShow,
      onHide,
      onUnload,
      onPullDownRefresh,
      onReachBottom,
      onShareAppMessage,
      onPageScroll,
      onResize,
      onTabItemTap
    } = args;

    // 重写 onLoad
    args.onLoad = function (opt) {
      this.loadTime = Date.now();

      // 自定义行为

      util.pushLog({
        page: this.route,
        name: 'page-loaded',
        time: this.loadTime
      });

      if (onLoad) {
        return onLoad.call(this, opt);
      }
    };

    // 重写 onReady
    args.onReady = function (opt) {
      this.readyTime = Date.now();

      // 自定义行为

      util.pushLog({
        page: this.route,
        name: 'page-dom-loaded',
        cost: this.readyTime - this.loadTime
      });

      if (onReady) {
        return onReady.call(this, opt);
      }
    };

    // 重写 onShow
    args.onShow = function (opt) {
      this.showTime = Date.now();

      // 自定义行为

      util.pushLog({
        page: this.route,
        name: 'page-visible',
        time: this.showTime
      });

      if (onShow) {
        return onShow.call(this, opt);
      }
    };

    // 重写onHide
    args.onHide = function (opt) {
      this.hideTime = Date.now();

      // 自定义行为

      util.pushLog({
        page: this.route,
        name: 'page-duration',
        cost: this.hideTime - this.showTime
      });

      if (onHide) {
        return onHide.call(this, opt);
      }
    };

    // 重写 onUnload
    args.onUnload = function (opt) {
      this.unloadTime = Date.now();

      // 自定义行为

      util.pushLog({
        page: this.route,
        name: 'page-unloaded',
        cost: this.hideTime - this.showTime
      });

      if (onUnload) {
        return onUnload.call(this, opt);
      }
    };

    // 重写 onPullDownRefresh
    args.onPullDownRefresh = function (opt) {
      this.pullDownRefreshTime = Date.now();

      // 自定义行为

      if (onPullDownRefresh) {
        return onPullDownRefresh.call(this, opt);
      }
    };

    // 重写 onReachBottom
    args.onReachBottom = function (opt) {
      this.reachBottomTime = Date.now();

      // 自定义行为

      if (onReachBottom) {
        return onReachBottom.call(this, opt);
      }
    };

    // 重写 onShareAppMessage
    args.onShareAppMessage = function (opt) {
      this.shareAppMessageTime = Date.now();

      // 自定义行为

      instance.addPageEvent({
        page: this.route,
        name: 'share-app-message',
        time: this.shareAppMessageTime,
      });

      if (onShareAppMessage) {
        return onShareAppMessage.call(this, opt);
      }
    };

    // 重写 onPageScroll
    args.onPageScroll = function (opt) {
      this.pageScrollTime = Date.now();

      // 自定义行为

      if (onPageScroll) {
        return onPageScroll.call(this, opt);
      }
    };

    // 重写 onResize
    args.onResize = function (opt) {
      this.resizeTime = Date.now();

      // 自定义行为

      if (onResize) {
        return onResize.call(this, opt);
      }
    };

    // 重写 onTabItemTap
    args.onTabItemTap = function (opt) {
      this.tabItemTapTime = Date.now();

      // 自定义行为

      if (onTabItemTap) {
        return onTabItemTap.call(this, opt);
      }
    };

    return oldPage.call(this, args);
  };
}

Page = getNewPage();